<?php
namespace app\plus_exam\model;
use think\Model;


/**
 *  考场统计
 * Class History
 * @package app\exam\model
 */

class History extends Model{


    protected $pk       = 'id';
    protected $table    = 'sp_plus_exam_history';
    protected $autoWriteTimestamp = true;
    protected $pageNumber = 20;

    /**
     * 单个
     * @param int $param
     * @return array
     */
    public function find_info($param) {
        return self::get($param);
    }

    /**
     * 列表
     * @access public
     * @param array $where
     */
    public function select_lists($where=null){
        return $this->where($where)->order('id desc')->paginate($this->pageNumber);
    }

    /**

     * [get_exam_history_by_user_id 获取考试分数的用户ID和课程ID]
     * @param  [int] $user_id   [用户ID]
     * @return [array]          [description]
     */
    public function get_exam_user_history($user_id = NULL,$where=null) {
        if($user_id){
            return $this->alias('A')
            ->join('plus_exam_school B','A.school_id = B.id','left')
            ->field('A.*,B.name')
            ->where(['user_id' => $user_id,'A.status' => 1])
            ->order('id desc')
            ->select();
        }else{
            return $this->alias('A')
            ->join('plus_exam_school B','A.school_id = B.id','left')
            ->join('passport_user C','A.user_id = C.id','left')
            ->field('A.*,B.name,C.nickname')
            ->where($where)
            ->order('id desc')
            ->paginate($this->pageNumber);
        }
    }

    /**
     * [get_exam_history_by_user_id 获取考试分数的用户ID和课程ID]
     * @param  [int] $user_id   [用户ID]
     * @return [array]          [description]
     */

    public function get_exam_user_history_nopage($where=null) {
        return $this->alias('A')
            ->join('plus_exam_school B','A.school_id = B.id','left')
            ->join('sp_passport_face_anyang C','A.user_id = C.user_id','left')
            ->field('A.*,B.name,C.name as username,C.work,C.phone')
            ->where($where)
            ->order('id desc')
            ->select();
    }
    

    /**
     * 前台
     * [get_exam_history_by_user_id 获取考试分数的用户ID和课程ID]
     * @param  [int] $user_id   [用户ID]
     * @return [array]          [description]
     */
    public function get_exam_history_by_user_id($param) {
        return $this->where($param)->order('create_time desc')->find();
    }


    /**
     * 前台
     * @access public
     * @param array $where
     */
    public function get_exam_history_by_user_id_lists($param){
        return $this->where($param)->order('id desc')->select();
    }
 

    /**
     * 添加
     * @param array $param
     * @return int
     */
    public function add_data($param) {
        return $this->insertGetId($param);
    }

    /**
     * 编辑
     * @param type $param
     */
    public function edit_data($param) {
        $data = [
            'category_id'     => $param['category_id'],
            'name'            => $param['name'],
            'start_time'      => $param['start_time'],
            'end_time'        => $param['end_time'],
            'is_repeat'       => $param['is_repeat'],
            'lock_time'       => $param['lock_time'],
            'start_lock_time' => $param['start_lock_time'],
            'question_number' => $param['question_number'],
            'one_point'       => $param['one_point'],
        ];
        $where = ['id'=>$param['id']];
        return $this->where($where)->update($data);
    }

    /**
     * 考试提交
     */
    public function submit_add($where,$data){
        return $this->where($where)->update($data);
    }

    /**
     * 删除数据
     * @param int $id
     */
    public function delete_data($id) {
        return $this->destroy($id);
    }
}